Skip to content

On MQTT Message

Plugin: MQTT | Mode: Listen

Subscribe to an MQTT topic and respond to incoming messages on that topic.

Settings

topic

The MQTT Topic that is subscribed to in order to respond to incoming messages.

Use wildcard topics (+ and #) to respond to messages in multiple (sub-)topics.

If you define a wildcard topic, the if condition allows you to specify with topic contains to which exact topic the respective condition refers.

if

Check incoming messages on the subscribed topic and trigger the next state if the condition matches.

In the Conditions chapter you can find out in more detail how to query incoming messages.

The value of the condition is the incoming message.

field

Use field to retrieve a specific value within a JSON message. You can use dot notation to specify deeper values in the JSON message.

To query the "humidity" value in the following JSON message

{
    "sensor":{
        "temperature":20,
        "humidity":120
    }
}

use the field with

sensor.humidity

Screenshot On MQTT Message action with field option

topic contains

If you have specified a wildcard topic, you can use topic contains to filter which topic should be responded to in this condition.

If the topic of the message contains the topic contains value, the condition will be reviewed. topic contains always filters the complete topic.

You can use Regular Expressions to specify the filter more precisely.

For example you can query if the topic contains a topic section only at the end. A message with topic garden/apple-tree/sensor/ph-value would be caught with the following topic contains:

ph-value$

Screenshot On MQTT Message action with topic contains option

else

Set the next state that will be triggered if none of the if conditions matched the received message or if you did not specify any if conditions.

If you did not specify an if condition, the system will switch to the next state for any message received on the specified topic or one of the topics corresponding to the wildcard.

Action data

You can access the last received MQTT message via the On MQTT Message action data variable. Use for example:

[[state.MyState.onMQTTMessage_1.message]]

To access the most recently received message of the top On MQTT Message action in the state MyState.

match

The message or the part of the message that has fulfilled an if condition in the On MQTT Message action.

Example:

part of the message

message

Total last received message on the topic that subscribed to the On MQTT Message action.

Example:

This is not the only part of the message.

topic

Exact topic as a single string, of the last received message, on the topic that subscribed to the On MQTT Message action.

Example:

some/topic

topic_levels

A list of all the individual topic levels of the most recently received message on the topic to which the On MQTT Message action is subscribed.

Example:

["some", "topic"]

topic_contains

The part of the topic that was matched with topic contains.

Example:

partof/topic

retain

Indicates whether the message has been retained, i.e. subsequently sent by the broker through the subscription.

Boolean Value (true/false)

qos

Indicates the Quality of Service level of the message.

Can be an integer value 0, 1 or 2.

dup

If true, the Message is a duplicate.

length

The message length.

Example

Take a look at the example in the MQTT Plugin chapter.